;; Copyright (c) 2012, 2013 Alan Watson. All rights reserved.

;; BSD-style license: http://synthcode.com/license.txt

(define-library (clock leap-seconds)

  ;;> \title{Library (clock leap-seconds)}
  ;;
  ;;> This library implements procedures that give the number of leap
  ;;> seconds before a specified instant.

  (export leap-seconds-before-utc-day
          leap-seconds-before-tai-second)

  (import (scheme base))
  (import (clock leap-seconds-list))

  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

  (begin

    (define (alist-scan key alist)
      (cond ((null? alist) 0)
            ((>= key (caar alist)) (cdar alist))
            (else (alist-scan key (cdr alist)))))

    ;;> \procedure{(leap-seconds-before-utc-day \var{utc-day})}
    ;;>
    ;;> Returns the number of leap seconds before the start of the UTC day
    ;;> \var{utc-day}.

    (define (leap-seconds-before-utc-day utc-day)
      (alist-scan utc-day
                  (leap-seconds-before-utc-day-alist)))

    ;;> \procedure{(leap-seconds-before-tai-seconds \var{tai-seconds})}
    ;;>
    ;;> Returns the number of leap seconds before the start of the TAI seconds
    ;;> \var{tai-seconds}.

    (define (leap-seconds-before-tai-second tai-second)
      (alist-scan tai-second
                  (leap-seconds-before-tai-second-alist)))))
